package com.yanqu.road.server.cross3server.cmd.dispersegold;

import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.cross.disperseGold.CrossDisperseGoldProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ChannelProtocol;
import com.yanqu.road.server.protocol.Cross3Protocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.ConfigHelper;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Cmd(code = Cross3Protocol.C3_DISPERSE_GOLD_SEND_INFO, desc = "跨地区送财（游戏服发送/跨地区返回）")
public class Cross3DisperseGoldCmd extends CrossCommand {

    private CrossDisperseGoldProto.CrossDisperseGoldMsg reqMsg;
    private PlayerProto.PlayerBaseTempMsg userBaseInfoMsg;
    private Set<Long> serverIds = new HashSet<>();

    private boolean syncChannel = false;

    private List<CrossDisperseGoldProto.CrossDisperseGoldGroupUserTemp> groupUserTempList;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = CrossDisperseGoldProto.CrossDisperseGoldMsg.parseFrom(packet.getBytes());
        userBaseInfoMsg = reqMsg.getPlayerInfo();
        serverIds.addAll(reqMsg.getServerIdsList());
        if (reqMsg.hasSyncChannel()) {
            syncChannel = reqMsg.getSyncChannel();
        }
        groupUserTempList = reqMsg.getGroupUserListList();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        // 转发到别的地区
        if (syncChannel) {
            CrossDisperseGoldProto.CrossDisperseGoldMsg.Builder builder = reqMsg.toBuilder();
            builder.setSyncChannel(false);
            YanQuMessage msg = YanQuMessageUtils.buildMessage(ChannelProtocol.N_CHANNEL_DISPERSE_GOLD_TRANSFER, builder);
            MessageHelper.sendChannelServer(msg);
        }

        // 本地区直接发

        CrossDisperseGoldProto.CrossDisperseGoldMsg.Builder msg = CrossDisperseGoldProto.CrossDisperseGoldMsg.newBuilder();
        msg.setInfo(reqMsg.getInfo());
        msg.setPlayerInfo(userBaseInfoMsg);
        msg.addAllServerIds(serverIds);
        msg.setSyncChannel(syncChannel);
        msg.addAllGroupUserList(groupUserTempList);
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_DISPERSEGOLD_INFO, msg);

        int channelId = ConfigHelper.getInt("channelId");
        for (Long serverId : serverIds) {
            int tmpChannelId = ServerListMgr.getChannelId(serverId);
            if (channelId == tmpChannelId) {
                MessageHelper.sendPacket(serverId, 0, pbMsg);
            }
        }
        // 再发送名单范围
        // 聚合一下,这边拿区服,具体名单到区服去拿就好了
        Set<Long> groupServerSet = new HashSet<>();
        for (CrossDisperseGoldProto.CrossDisperseGoldGroupUserTemp groupUserTemp : groupUserTempList) {
            if (serverIds.contains(groupUserTemp.getServerId())) {
                continue;
            }
            groupServerSet.add(groupUserTemp.getServerId());
        }
        for (Long serverId : groupServerSet) {
            int tmpChannelId = ServerListMgr.getChannelId(serverId);
            if (channelId == tmpChannelId) {
                MessageHelper.sendPacket(serverId, 0, pbMsg);
            }
        }
    }

    @Override
    public void sendData() {

    }
}
